/*
 * WriteCodeDialog.java
 */

package userInterface;

import java.math.BigInteger;

public class WriteCodeDialog extends javax.swing.JDialog {

    public String message = null;
    public String prime = null;
    public int shares = 0;
    public int threshold = 0;

    /** Creates new form WriteCodeDialog */
    public WriteCodeDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldMessage = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldShares = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldThreshold = new javax.swing.JTextField();
        jButtonWriteCodeOk = new javax.swing.JButton();
        jButtonWriteCodeCancel = new javax.swing.JButton();
        jTextFieldPrime = new javax.swing.JTextField();
        jButtonGetPrime = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Secret message");

        jLabel2.setText("Shares");

        jLabel3.setText("Threshold");

        jButtonWriteCodeOk.setText("Ok");
        jButtonWriteCodeOk.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButtonWriteCodeOkMousePressed(evt);
            }
        });

        jButtonWriteCodeCancel.setText("Cancel");
        jButtonWriteCodeCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButtonWriteCodeCancelMousePressed(evt);
            }
        });

        jButtonGetPrime.setText("Generate prime");
        jButtonGetPrime.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButtonGetPrimeMousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jButtonGetPrime))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jButtonWriteCodeOk, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonWriteCodeCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextFieldMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                    .addComponent(jTextFieldThreshold, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                    .addComponent(jTextFieldShares, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                    .addComponent(jTextFieldPrime))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldShares, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldThreshold, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPrime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonGetPrime))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonWriteCodeOk)
                    .addComponent(jButtonWriteCodeCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonWriteCodeOkMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonWriteCodeOkMousePressed
        message = jTextFieldMessage.getText();
        prime = jTextFieldPrime.getText();
        if (jTextFieldShares.getText() != null) {
            shares = Integer.parseInt(jTextFieldShares.getText());
        }
        if (jTextFieldThreshold.getText() != null) {
            threshold = Integer.parseInt(jTextFieldThreshold.getText());
        }
        dispose();
    }//GEN-LAST:event_jButtonWriteCodeOkMousePressed

    private void jButtonWriteCodeCancelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonWriteCodeCancelMousePressed
        dispose();
    }//GEN-LAST:event_jButtonWriteCodeCancelMousePressed

    private void jButtonGetPrimeMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonGetPrimeMousePressed
        BigInteger generatedPrime;
        if (jTextFieldPrime.getText().equals("")) {
            message = jTextFieldMessage.getText();
            generatedPrime = (new BigInteger(message)).nextProbablePrime();
        } else {
            generatedPrime = (new BigInteger(jTextFieldPrime.getText())).nextProbablePrime();
        }
        jTextFieldPrime.setText(generatedPrime.toString());
    }//GEN-LAST:event_jButtonGetPrimeMousePressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                WriteCodeDialog dialog = new WriteCodeDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);




                    }
                });
                dialog.setVisible(true);




            }
        });




    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonGetPrime;
    private javax.swing.JButton jButtonWriteCodeCancel;
    private javax.swing.JButton jButtonWriteCodeOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextFieldMessage;
    private javax.swing.JTextField jTextFieldPrime;
    private javax.swing.JTextField jTextFieldShares;
    private javax.swing.JTextField jTextFieldThreshold;
    // End of variables declaration//GEN-END:variables
}
